<?
/**
 * article页面的功能处理类,处理由article页面内需要的功能,返回处理结果
 * 
 * @copyright Copyright (c) 2008 sdmblog (http://www.sdmblog.cn)
 * @author sdomain(sdomain@live.cn)
 * @version article.class.php Sat May 17 17:04:45 CST 2008 php $
 */
include_once('../model/article.model.php');
include_once ('../xajax/inc/xajax.inc.php');
include_once('../include/ajax.common.php');

$xajax = new xajax('article.class.php');
//文章回复
$xajax->registerFunction('getArticleComment');
//分类列表
$xajax->registerFunction('getCategoryList');
//日历
$xajax->registerFunction('getCalendar');
//显示最新文章
$xajax->registerFunction('getRecentArticles');
//显示回复
$xajax->registerFunction('getComment');
//网站统计
$xajax->registerFunction('getStats');
//友情连接
$xajax->registerFunction('getLinks');
//用户登录
$xajax->registerFunction('userLogin');
//文章读取记数器
$xajax->registerFunction('addViews');
//发表文章
$xajax->registerFunction('newArticle');
//发表回复
$xajax->registerFunction('addComment');

/**
 * 获取指定文章的回复信息
 *
 * @param int $aid 文章编号
 * @return ajaxobj
 */
function getArticleComment($aid)
{
	$obj = new xajaxResponse();
	try {
		$aid = (is_numeric($aid)&&$aid>0)?$aid:1;
		$article = new model_article();
		$data = $article->getArticleComment($aid);
		$strHTML = $article->getArticleCommentHtml($data);
		//$obj->alert($article->rowCount);
		$obj->assign('commentWrapper','innerHTML',$strHTML);
	}catch (Exception $e){
		$obj->alert('操作出错，出错信息：'.$e->getMessage());
	}
	return $obj;
}
/**
 * 获取分类列表
 *
 * @return ajaxObj
 */
function getCategoryList()
{
	$obj = new xajaxResponse();
	try {
		include_once('../model/index.model.php');
		$index = new model_index();
		$category = $index->getCategoryList();
		$article = new model_article();
		$strHTML = $article->getCategoryHtml($category);
		$index = null;
		$article = null;
		$obj->assign('panelCategoryContent','innerHTML',$strHTML);
		unset($category,$strHTML);
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
/**
 * 获取日历
 * @param int $year 年份
 * @param int $month 月份
 * @return ajaxObj
 */
function getCalendar($year,$month)
{
	$obj = new xajaxResponse();
	try {
		if (!is_numeric($year)||!is_numeric($month)) {
			$obj->alert('无效日期');
			return $obj;
		}
		if ($year>3000 || $year < 1900) {
			$obj->alert('无效日期');
			return $obj;
		}
		if ($month<1 || $month>12) {
			$obj->alert('无效日期');
			return $obj;
		}
		include_once('../model/index.model.php');
		$index = new model_index();
		$date = $index->getMonthArticleList($year,$month);
		$article = new model_article();
		$strHTML = $article->getCalendarHtml($year,$month,$date);
		$obj->assign('panelCalendar','innerHTML',$strHTML);
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
/**
 * 获取最近发表的文章（10个）
 *
 * @return ajaxObj
 */
function getRecentArticles()
{
	$obj = new xajaxResponse();
	try {
		include_once('../model/index.model.php');
		$index = new model_index();
		$recentArticle = $index->getRecentArticles();
		$strHTML = $index->getRecentHtml($recentArticle);
		$obj->assign('panelArticleContent','innerHTML',$strHTML);
		//$obj->alert($strHTML);
	}
	catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
/**
 * 文章回复列表-10个最新
 * 
 * @return ajaxObj
 */
function getComment()
{
	$obj = new xajaxResponse();
	try {
		include_once('../model/index.model.php');
		$index = new model_index();
		$comment = $index->getComment();
		$strHTML = $index->getCommentHtml($comment);
		$obj->assign('panelCommentContent','innerHTML',$strHTML);
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
/**
 * 网站统计及状态数据
 *
 * @return ajaxObj
 */
function getStats()
{
	$obj = new xajaxResponse();
	try {
		include_once('../model/index.model.php');
		$index = new model_index();
		$stats = $index->getStatsData();
		$strHTML = $index->getStatsHtml($stats);
		$obj->assign('panelStatsContent','innerHTML',$strHTML);
		//$obj->alert('sdf');
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
/**
 * 友情连接
 *
 * @return ajaxObj
 */
function getLinks()
{
	$obj = new xajaxResponse();
	try {
		$obj->assign('panelLinksContent','innerHTML','友情连接');
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;	
}
/**
 * 用户登录
 *
 * @param array $loginInfo 登录时输入的登录信息
 * @return xajaxObj
 */
function userLogin($loginInfo)
{
	$obj = new xajaxResponse();
	try{
		session_start();
		global $virdir;
		$username = $loginInfo['username'];
		$authnum = $loginInfo['authnum'];
		if($authnum!=$_SESSION['auth_num']){
			$obj->alert('请输入正确的验证码');
			$obj->assign('Loginbtn','disabled',false);
			return $obj;
		}
		if(!isUserName($username)){
			$obj->alert('非法的用户名，请输入合法的用户名；');
			$obj->assign('Loginbtn','disabled',false);
			return $obj;
		}
		include_once('../model/user.model.php');
		$user = new model_user;
		$res = $user->login($username,$loginInfo['password']);
		if($res!=1){
			$obj->assign('Loginbtn','disabled',false);
			$obj->alert('用户名或密码错误，请重新登录；');
			refreshAuthnum('authcode',$obj);
			return $obj;
		}
		$strHTML = $user->getUserHTML($user->userInfo);
		//$userHTML = $_COOKIE
		$obj->assign('panel_user','innerHTML',$strHTML);
		$obj->assign('panel_user','style.display','block');
		$obj->assign('panelUser','style.display','none');
		$obj->assign('panelUser','innerHTML','');
		$obj->assign('dologin','style.display','none');
		
		
		$authHTML = '<INPUT class=text maxLength=4 size=4 name=scode>
					<script>document.write("<s"+"cript src=\'/'.$virdir.'include/makecode.php?id=1\' type=\'text/javascript\' language=\'javascript\'></s"+"cript>")</script>
					<SPAN class=comment-text>* Please Enter the Security Code</SPAN>
					';
		$obj->assign('commentauthcode','innerHTML',$authHTML);
		
		//$obj->redirect();
	}catch(Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
/**
 * 阅读计数
 *
 * @param int $aid 文章编号
 * @return xajaxObj
 */
function addViews($aid)
{
	$obj = new xajaxResponse();
	try {
		$article = new model_article();
		$article->addViews($aid);
		//$obj->alert($article->queryString);
		//$obj->alert('sdm');
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
/**
 * 发表新贴
 *
 * @param array $articleinfo 由xajax转换过来的贴子信息的数组
 * @return xajaxObj
 */
function newArticle($articleinfo)
{
	$obj = new xajaxResponse();
	try {
		global $virdir,$ip;
		$logininfo = islogin();
		if ($logininfo==0) {
			$obj->alert('请登录');
			$obj->redirect('/'.$virdir.'index.php');
			return $obj;
		}else {
			if (checkRight('NEW_ARTICLE',$userinfo)!=1) {
				$obj->alert('你没有权限进行此操作');
				$obj->redirect('/'.$virdir.'index.php');
				return $obj;
			}
			
		}
		
		if (empty($articleinfo['log_title'])||empty($articleinfo['message'])||empty($articleinfo['log_catid'])) {
			$obj->alert('请输入文章的完整信息：标题/文章分类/文章内容');
			$obj->assign('btnSubmit','disabled',false);
			return $obj;
		}
		$articleinfo['author'] = $logininfo[1];
		$articleinfo['message'] = addslashes($articleinfo['message']);
		$article = new model_article();
		$articleid = $article->saveArticle($articleinfo);
		$obj->alert('发表成功!');
		$obj->redirect('/'.$virdir.'article.php/id/'.$articleid);
		unset($GLOBALS['virdir']);
		unset($GLOBALS['ip']);
		unset($article);
		unset($articleid);
		unset($articleinfo);
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
function addComment($commInfo)
{
	$obj = new xajaxResponse();
	try {
		global $virdir,$ip;
		session_start();
		if (trim($commInfo['message'])=="") {
			$obj->alert('请输入评论内容');
			$obj->assign('btnSubmit','disabled',false);
			return $obj;
		}
		$authnum = $commInfo['authnum'];
		if ($_SESSION['auth_num']!=$authnum) {
			$obj->alert('验证码不正确，请重新输入');
			$obj->assign('btnSubmit','disabled',false);
			return $obj;
		}
		$right = checkRight('ADD_COMMENT',$userInfo);
		//$obj->alert(implode('-',$userInfo));
		if ($userInfo==0||$right==0) {
			$obj->alert('你当前的用户组没有此权限进行此操作');
			$obj->redirect('/'.$virdir.'index.php');
			return $obj;
		}
		$commInfo['author'] = $userInfo[0];
		$commInfo['submitip'] = $ip;
		$commInfo['message'] = addslashes($commInfo['message']);
		
		$article = new model_article();
		$article->addComment($commInfo);
		$obj->alert('添加成功');
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	$obj->assign('btnSubmit','disabled',false);
	return $obj;
}
$xajax->processRequest();
?>